<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				initValue:{
					reviewerStr : [],
					reviewerStr1 : [],
					reviewerStr2 :[]
				},
				userCacheKey1:"hr-train-trainApply-user-cache2",
				userCacheKey2:"hr-train-trainApply-user-cache3",
				userCacheKey3:"hr-train-trainApply-user-cache4",
				relevantAbilityCacheKey:"hr-train-trainApplay-relevantAvility-cache",
				partakeUserCache:"hr-train-trainApplay-partakeUser-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId',"orgId"])
		},
		methods:{
			formSubmit(){
				let _that = this
				_that.formdata.orgId = _that.orgId
				if(_that.formdata.userId1 && _that.formdata.userName1){
					_that.formdata.reviewerStr.push({
						userName:_that.formdata.userName1,
						postName:_that.formdata.postName1,
						userId:_that.formdata.userId1,
						postId:_that.formdata.postId1
					})
					_that.formdata.reviewerStr = JSON.stringify(_that.formdata.reviewerStr)
				}else{
					_that.formdata.reviewerStr = "[]"
				}
				if(_that.formdata.userId2 && _that.formdata.userName2){
					_that.formdata.reviewerStr1.push({
						userName:_that.formdata.userName2,
						postName:_that.formdata.postName2,
						userId:_that.formdata.userId2,
						postId:_that.formdata.postId2
					})
					_that.formdata.reviewerStr1 = JSON.stringify(_that.formdata.reviewerStr1)
				}else{
					_that.formdata.reviewerStr1 = "[]"
				}
				if(_that.formdata.userId3 && _that.formdata.userName3){
					_that.formdata.reviewerStr2.push({
						userName:_that.formdata.userName3,
						postName:_that.formdata.postName3,
						userId:_that.formdata.userId3,
						postId:_that.formdata.postId3
					})
					_that.formdata.reviewerStr2 = JSON.stringify(_that.formdata.reviewerStr2)
				}else{
					_that.formdata.reviewerStr2 = "[]"
				}
				if(_that.edit){
					_that.om.formSave("trainApply/doUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("trainApply/doAdd",_that.formdata,_that)
				}
			},
			getPostList(id,title,postId,postName){
				let _that = this 
				_that.om.request(
					"orgInstPosUserOfficial/getPostList",
					{
						"user.id":id
					}
				).then(res=>{
					if(res.length){
						_that.$refs.autoform.changeEditor(title,postId,"data",configSelectData(res,"id","posname",true))
						_that.$refs.autoform.formdata[postId]=res[0].id
						_that.$refs.autoform.formdata[postName]=res[0].posname
					}else{
						_that.om.toast("该人员在系统中无任职岗位",3000)
					}
				})
			},
			intLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.sysdictComboboxUrl("38404")
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"trainApply/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("title1","trainTypeId","data",configSelectData(res[1],"id","dicname"),mainData)
					changeEditor('title2', 'userId1', {
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({
							}),
							namefield:urlUtil.userPageUrl().namefield,
							searchfield:"q",
							initload:true,
							url:urlUtil.userPageUrl().url,
							cachename:_that.userCacheKey1,
							placeholder:"请输入姓名"
						}
					},null,mainData)
					changeEditor('title3', 'userId2', {
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({
							}),
							namefield:urlUtil.userPageUrl().namefield,
							searchfield:"q",
							initload:true,
							url:urlUtil.userPageUrl().url,
							cachename:_that.userCacheKey2,
							placeholder:"请输入姓名"
						}
					},null,mainData)
					changeEditor('title4', 'userId3', {
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({
							}),
							namefield:urlUtil.userPageUrl().namefield,
							searchfield:"q",
							initload:true,
							url:urlUtil.userPageUrl().url,
							cachename:_that.userCacheKey3,
							placeholder:"请输入姓名"
						}
					},null,mainData)
					changeEditor('title1', 'relevantAbilityIdStr', {
						"linkParam": {
							other: JSON.stringify({
								code: '35905'
							}),
							namefield: 'text',
							likefield: 'text',
							initload: true,
							url: '/tsysdict/findLastChildTree',
							cachename: _that.relevantAbilityCacheKey
						}
					}, null, mainData);
					changeEditor('title1', 'partakeUserIdStr', {
						linkParam: {
							namefield: urlUtil.userPageUrl().namefield,
							searchfield: "q",
							initload:true,
							url: urlUtil.userPageUrl().url,
							cachename: _that.partakeUserCache,
							placeholder: urlUtil.userPageUrl().prompt,
							pagination: true
						}
					}, null, mainData);
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[2].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value[index])
								_that.$set(_that.formdata,index,res[2].value[index])
							}
						}
						if(res[2].value.reviewerList.length){
							_that.$refs.autoform.formdata.userId1 = res[2].value.reviewerList[0].userId
							_that.$refs.autoform.formdata.userName1 = res[2].value.reviewerList[0].userName
							_that.$refs.autoform.formdata.postId1 = res[2].value.reviewerList[0].id
							_that.$refs.autoform.formdata.postName1 = res[2].value.reviewerList[0].postName
							_that.getPostList(res[2].value.reviewerList[0].userId,"title2","postId1","postname1")
						}
						if(res[2].value.reviewer1List.length){
							_that.$refs.autoform.formdata.userId2 = res[2].value.reviewer1List[0].userId
							_that.$refs.autoform.formdata.userName2 = res[2].value.reviewer1List[0].userName
							_that.$refs.autoform.formdata.postId2 = res[2].value.reviewer1List[0].id
							_that.$refs.autoform.formdata.postName2 = res[2].value.reviewer1List[0].postName
							_that.getPostList(res[2].value.reviewer1List[0].userId,"title3","postId2","postname2")
						}
						
						if(res[2].value.reviewer2List.length){
							_that.$refs.autoform.formdata.userId3 = res[2].value.reviewer2List[0].userId
							_that.$refs.autoform.formdata.userName3 = res[2].value.reviewer2List[0].userName
							_that.$refs.autoform.formdata.postId3 = res[2].value.reviewer2List[0].id
							_that.$refs.autoform.formdata.postName3 = res[2].value.reviewer2List[0].postName
							_that.getPostList(res[2].value.reviewer2List[0].userId,"title4","postId3","postname3")
						}
						_that.$nextTick(function(){
							let d  = _that.$refs.autoform.formdata
							_that.om.request(
								"tsysdict/getComboTree", {
									code: "35905"
								}
							).then(res => {
								let newArr = []
								if(d.relevantAbilityIdStr != null){
									let relevantAbilityArr = d.relevantAbilityIdStr.split(",")
									for(let obj of res){
										if(relevantAbilityArr.indexOf(obj.id+"") > -1){
											newArr.push(obj)
										}
									}
									_that.$refs.autoform.changeEditor('title1', 'relevantAbilityIdStr', {
										initSelect: newArr
									})
								}
							})
							_that.$refs.autoform.initSelectIndex("title2","postId1")
							_that.$refs.autoform.initSelectIndex("title3","postId2")
							_that.$refs.autoform.initSelectIndex("title4","postId3")
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
							_that.$refs.autoform.initSelectIndex("title1","trainTypeId")
							
						})
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.userCacheKey1,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userId1 = d.id 
							formdata.userName1 = d.username
							_that.getPostList(d.id,"title2","postId1","postName1")
							uni.removeStorage({
								key:_that.userCacheKey1
							})
						}
					}
				})
				uni.getStorage({
					key:_that.userCacheKey2,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userId2 = d.id 
							formdata.userName2 = d.username
							_that.getPostList(d.id,"title3","postId2",'postName2')
							uni.removeStorage({
								key:_that.userCacheKey2
							})
						}
					}
				})
				uni.getStorage({
					key:_that.userCacheKey3,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userId3 = d.id 
							formdata.userName3 = d.username
							_that.getPostList(d.id,"title4","postId3","postName3")
							uni.removeStorage({
								key:_that.userCacheKey3
							})
						}
					}
				})
				uni.getStorage({
					key:_that.relevantAbilityCacheKey,
					success:(res)=>{
						if(res.data){
							let formdata = _that.$refs.autoform.formdata
							let d = res.data
							formdata.relevantAbilityIdStr = d.map(e => e.id).join(",")
							formdata.relevantAbilityNameStr = d.map(e => e.text).join(",")
							_that.$refs.autoform.changeEditor('title1', 'relevantAbilityIdStr', {
								initSelect: d
							})
							uni.removeStorage({
								key:_that.relevantAbilityCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.partakeUserCache,
					success:(res)=>{
						let formdata = _that.$refs.autoform.formdata
						let d = res.data
						formdata.partakeUserIdStr = d.map(e => e.id).join(",")
						formdata.partakeUserNameStr = d.map(e => e.username).join(",")
						_that.$refs.autoform.changeEditor('title1', 'partakeUserIdStr', {
							initSelect: d
						})
						uni.removeStorage({
							key:_that.partakeUserCache
						})
					}
				})
				
			}
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
